import {
  loginByUsername,
  logout,
  getUserInfo
} from "@/api/login"
import {
  setToken,
  removeToken
} from "@/utils/auth"
import utils from "@/utils/utils"

const user = {
  state: {
    user: "",
    status: "",
    code: "",
    userTokem: "",
    token: "",
    name: "",
    avatar: "",
    introduction: "",
    roles: [],
    userInfo: {}, // 用户信息
    setting: {
      articlePlatform: []
    }
  },

  mutations: {
    SET_CODE: (state, code) => {
      state.code = code
    },
    SET_USERTOKEN: (state, userTokem) => {
      state.userTokem = userTokem
    },
    SET_TOKEN: (state, token) => {
      state.token = token
    },
    SET_INTRODUCTION: (state, introduction) => {
      state.introduction = introduction
    },
    SET_SETTING: (state, setting) => {
      state.setting = setting
    },
    SET_STATUS: (state, status) => {
      state.status = status
    },
    SET_NAME: (state, name) => {
      state.name = name
    },
    SET_AVATAR: (state, avatar) => {
      state.avatar = avatar
    },
    SET_ROLES: (state, roles) => {
      state.roles = roles
    },
    SET_UERINFO: (state, userInfo) => {
      state.userInfo = userInfo
    }
  },

  actions: {
    // 用户名登录
    LoginByUsername({
      commit
    }, userInfo) {
      const username = userInfo.username.trim()
      return new Promise((resolve, reject) => {
        // c(username, userInfo.password).then(response => {
        var response = {}
        response.data = {
          avatar: "https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif",
          introduction: "我是超级管理员",
          name: "Super Admin",
          role: ["admin"],
          token: "admin"
        }
        //         loginByUsername(username, userInfo.password).then(response => {
        const data = response.data
        setToken(response.data.token)
        commit("SET_TOKEN", data.token)
        resolve()
        // }).catch(error => {
        //   reject(error)
        // })
      })
    },

    // 获取用户信息
    GetUserInfo({
      commit,
      state
    }) {
      return new Promise((resolve, reject) => {
        // getUserInfo(state.token).then(response => {
        var response = {}
        var ss = user.state.name
        var tt = state.token
        // getsdcxxx
        response.data = utils.getUserInfo(tt)
        const data = response.data
        commit("SET_ROLES", data.role)
        commit("SET_NAME", data.name)
        commit("SET_AVATAR", data.avatar)
        commit("SET_INTRODUCTION", data.introduction)
        resolve(response)
        // resolve(getsdcxxx)
        // }).catch(error => {
        //   reject(error)
        // })
      })
    },

    // 第三方验证登录
    // LoginByThirdparty({ commit, state }, code) {
    //   return new Promise((resolve, reject) => {
    //     commit('SET_CODE', code)
    //     loginByThirdparty(state.status, state.email, state.code).then(response => {
    //       commit('SET_TOKEN', response.data.token)
    //       setToken(response.data.token)
    //       resolve()
    //     }).catch(error => {
    //       reject(error)
    //     })
    //   })
    // },

    // 登出
    LogOut({
      commit,
      state
    }) {
      return new Promise((resolve, reject) => {
        commit("SET_TOKEN", "")
        commit("SET_ROLES", [])
        removeToken()
        resolve()
      })
    },

    // 前端 登出
    FedLogOut({
      commit
    }) {
      return new Promise(resolve => {
        commit("SET_TOKEN", "")
        removeToken()
        resolve()
      })
    },

    // 动态修改权限
    ChangeRole({
      commit
    }, role) {
      return new Promise(resolve => {
        commit("SET_TOKEN", role)
        setToken(role)
        getUserInfo(role).then(response => {
          const data = response.data
          commit("SET_ROLES", data.role)
          commit("SET_NAME", data.name)
          commit("SET_AVATAR", data.avatar)
          commit("SET_INTRODUCTION", data.introduction)
          resolve()
        })
      })
    }
  }
}

export default user
